{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 题目1",
   "id": "9494fb370e1ef01a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T01:23:21.037160Z",
     "start_time": "2025-07-30T01:23:20.950222Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "题目1: 温度数据分析\n",
    "A城市一周的气温为[28, 30, 29, 31, 32, 30, 29]\n",
    "- 计算平均气温，最高气温和最低气温。\n",
    "- 找出气温超过 30°C 的天数 \n",
    "\"\"\"\n",
    "import numpy as np\n",
    "temps = np.array([28, 30, 29, 31, 32, 30, 29])\n",
    "print(f'temps: {temps}')\n",
    "print(f'average temp is {round(np.average(temps), 2)}')\n",
    "print(f'max temp is {np.max(temps)}')\n",
    "print(f'min temp is {np.min(temps)}')\n",
    "print(f'气温超过30度的天数有： { len(temps[temps > 30]) }')\n",
    "temps > 30"
   ],
   "id": "5dbe125e7d1eb2f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "temps: [28 30 29 31 32 30 29]\n",
      "average temp is 29.86\n",
      "max temp is 32\n",
      "min temp is 28\n",
      "气温超过30度的天数有： 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True,  True, False, False])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T02:28:06.409269Z",
     "start_time": "2025-07-30T02:28:06.392314Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "题目2：\n",
    "班级成绩：[85, 90, 78, 92, 88]\n",
    "- 计算成绩平均分、中位数、方差、标准差\n",
    "将成绩转换为十分制下的成绩\n",
    "\"\"\"\n",
    "scores = np.array([85, 90, 78, 92, 88])\n",
    "print(scores)\n",
    "print(np.average(scores))\n",
    "print(np.median(scores))\n",
    "print(np.var(scores))\n",
    "print(np.std(scores))\n",
    "# np.random.rand()"
   ],
   "id": "b3a9c0cc6faf4552",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[85 90 78 92 88]\n",
      "86.6\n",
      "88.0\n",
      "23.84\n",
      "4.882622246293481\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.5288949197529045"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T02:53:29.061277Z",
     "start_time": "2025-07-30T02:53:29.037325Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "题目3：\n",
    "随机数据生成\n",
    "生成一个3行4列的随机整数数组，其中数据元素范围为[0, 10)\n",
    "- 计算每列的最大值和每行的最小值\n",
    "- 将数组中的所有技术替换为-1\n",
    "\"\"\"\n",
    "np.random.seed(0) # 播种一个随机种子，其编号为0\n",
    "arr = np.random.randint(1, 10, 12).reshape(3, 4)\n",
    "print(arr)\n",
    "print(np.max(arr, axis=0))\n",
    "print(np.max(arr, axis=1))\n",
    "print(np.where(arr % 2 == 1, -1, arr)) # arr % 2 == 1 若为真，取-1，否则取arr中对应元素\n",
    "print(np.select([arr % 2 == 1], [-1], default=arr)) # 当不符合condition时，默认值从arr中获取\n",
    "print(f'--------')\n",
    "arr[arr % 2 == 1] = - 1\n",
    "print(arr)\n",
    "print(arr % 2 == 1)"
   ],
   "id": "d69345c1da6f0fd2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 1 4 4]\n",
      " [8 4 6 3]\n",
      " [5 8 7 9]]\n",
      "[8 8 7 9]\n",
      "[6 8 9]\n",
      "[[ 6 -1  4  4]\n",
      " [ 8  4  6 -1]\n",
      " [-1  8 -1 -1]]\n",
      "[[ 6 -1  4  4]\n",
      " [ 8  4  6 -1]\n",
      " [-1  8 -1 -1]]\n",
      "--------\n",
      "[[ 6 -1  4  4]\n",
      " [ 8  4  6 -1]\n",
      " [-1  8 -1 -1]]\n",
      "[[False  True False False]\n",
      " [False False False  True]\n",
      " [ True False  True  True]]\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T03:59:09.788168Z",
     "start_time": "2025-07-30T03:59:09.780191Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1, 10, (2, 5))\n",
    "print(arr)\n",
    "np.reshape(arr, (2, 5))"
   ],
   "id": "d5c8d6108146dd4d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 9 5 4 8]\n",
      " [6 6 1 2 6]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[5, 9, 5, 4, 8],\n",
       "       [6, 6, 1, 2, 6]], dtype=int32)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T04:02:39.935437Z",
     "start_time": "2025-07-30T04:02:39.926327Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1, 10, (2, 5))\n",
    "print(arr)\n",
    "\"\"\"\n",
    "在二维矩阵当中，轴0为竖直方向，轴1为水平方向\n",
    "\"\"\"\n",
    "print(f'每行的和: {np.sum(arr, axis=1)}')\n",
    "print(f'每列的均值: {np.average(arr, axis=0)}')\n",
    "print(arr.flatten())\n",
    "print(arr)"
   ],
   "id": "6bf48208c2d5cc33",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9 6 1 7 6]\n",
      " [4 2 9 1 5]]\n",
      "每行的和: [29 21]\n",
      "每列的均值: [6.5 4.  5.  4.  5.5]\n",
      "[9 6 1 7 6 4 2 9 1 5]\n",
      "[[9 6 1 7 6]\n",
      " [4 2 9 1 5]]\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T05:20:55.739142Z",
     "start_time": "2025-07-30T05:20:55.716203Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "\n",
    "\"\"\"\n",
    "arr = np.random.randint(0, 20, (5, 5))\n",
    "print(arr)\n",
    "print(arr[arr>10])\n",
    "arr[arr>10] = 0 # 将arr中大于10的元素改为0\n",
    "print(arr)\n",
    "\n",
    "np.argmax(arr)"
   ],
   "id": "59ac1691254a23dc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[19 11 18 13  1]\n",
      " [ 6 10 16 16  3]\n",
      " [17 10  9 12 13]\n",
      " [ 6  6  7  8 13]\n",
      " [ 7 16 15  8  3]]\n",
      "[19 11 18 13 16 16 17 12 13 13 16 15]\n",
      "[[ 0  0  0  0  1]\n",
      " [ 6 10  0  0  3]\n",
      " [ 0 10  9  0  0]\n",
      " [ 6  6  7  8  0]\n",
      " [ 7  0  0  8  3]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.int64(6)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T05:51:12.203245Z",
     "start_time": "2025-07-30T05:51:12.180345Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将2个数组进行水平和垂直拼接\n",
    "arr1 = np.array([1, 2, 3])\n",
    "arr2 = np.array([4, 5, 6])\n",
    "arr = np.concatenate((arr1, arr2))\n",
    "print(arr)\n",
    "arr3 = np.reshape(arr, (2, 3))\n",
    "print(arr3)"
   ],
   "id": "49185b9a90c7f6f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T05:55:26.632856Z",
     "start_time": "2025-07-30T05:55:26.611450Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([2, 1, 2, 3, 1, 4, 3])\n",
    "print(np.unique(arr))\n",
    "print(np.unique(arr, return_counts=True)) # 获得去重后的数组 并且额外返回一个元素重复的次数"
   ],
   "id": "a196f3549740f047",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "(array([1, 2, 3, 4]), array([2, 2, 2, 1]))\n"
     ]
    }
   ],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-30T09:15:04.503133Z",
     "start_time": "2025-07-30T09:15:04.488174Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "题目20：\n",
    "A商店5天的销售额和成本如下（单位：万元）：\n",
    "销售额: [20, 25, 22, 30, 28]\n",
    "成本：[15, 18, 16, 22, 20]\n",
    "- 计算每天额的利润 (销售额 - 成本)\n",
    "- 计算利润的均值和标准差\n",
    "- 找出利润最高的天数\n",
    "\"\"\"\n",
    "sales = np.array([20, 25, 22, 30, 28])\n",
    "costs = np.array([15, 18, 16, 22, 20])\n",
    "profits = sales - costs\n",
    "print(profits)\n",
    "max = np.max(profits)\n",
    "\n",
    "print(f'利润最高的天数：{len(profits[ max == profits ])}')\n",
    "# np.where(profits == max) 是 NumPy 中查找满足条件的元素索引位置的标准方式。\n",
    "max_indices = np.where(profits == max)[0]\n",
    "print(f'最大利润对应的天数：{max_indices + 1}')\n"
   ],
   "id": "2a623f4c4e84a5e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 6 8 8]\n",
      "利润最高的天数：2\n",
      "最大利润对应的天数：[4 5]\n"
     ]
    }
   ],
   "execution_count": 79
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
